//
//  SportsGuideViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-30.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "SportsGuideViewController.h"
#import "SportsGuideCell.h"

@interface SportsGuideViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *mainTable;
    
    BOOL isFirst;
}
@property (nonatomic,retain) NSArray *dataArray;
@end

@implementation SportsGuideViewController

@synthesize dataArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated
{
    for (UIView *view in navigationBarView.subviews) {
        [view removeFromSuperview];
    }
    [navigationBarView setTitle:@"运动指导"];
    [self showNavbarStyle];
}

-(void)showView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, 66)];
    bgView.backgroundColor = GET_COLOR(@"ffc5db");
    [self.view addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"运动指导页-时间icon"]];
    [imageView sizeToFit];
    [imageView setCenter:CGPointMake(19+imageView.frame.size.width/2, bgView.frame.size.height/2)];
    [bgView addSubview:imageView];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
    [label1 setBackgroundColor:[UIColor clearColor]];
    label1.textColor = [UIColor whiteColor];
    label1.font = [UIFont systemFontOfSize:24];
    label1.text = [z_DateTime encodeTime:[NSDate date] format:@"yyyy年MM月dd日"];
    [label1 sizeToFit];
    [label1 setCenter:CGPointMake(imageView.frame.size.width+imageView.frame.origin.x+18+label1.frame.size.width/2, bgView.frame.size.height/2)];
    [bgView addSubview:label1];
    
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-(bgView.frame.size.height+bgView.frame.origin.y))];
    [mainTable setDelegate:self];
    [mainTable setDataSource:self];
    [mainTable setSeparatorColor:[UIColor clearColor]];
    [mainTable setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:mainTable];
    
    CGFloat width = 2.0;
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width/2+imageView.frame.origin.x-width/2, imageView.frame.origin.y+imageView.frame.size.height+NAVIGATIONBAR_HEIGHT-2, width, CURRENT_CONTENT_HEIGHT-(imageView.frame.origin.y+imageView.frame.size.height+NAVIGATIONBAR_HEIGHT))];
    lineView.backgroundColor = GET_COLOR(@"f2f2f2");
    [self.view addSubview:lineView];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!isFirst) {
        isFirst = YES;
        [self againLoading];
    }
}

//重新加载数据
-(void)againLoading
{
    [self showProgressView:nil belowSubview:navigationBarView];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:IOS_SPORTCOACHING forKey:IOS_METHOD_NAME];
    [dic setValue:APP_USER_MSG.id forKey:@"user_id"];
    
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_SPORTCOACHING_TAG need:NO];
    [self.netArray addObject:request];
    [request startAsynchronous];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.section];
    SportsGuideCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        
        cell = [[SportsGuideCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier size:[tableView rectForRowAtIndexPath:indexPath].size];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setContent:[dataArray objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@",[request responseString]);
    id result = [[request responseString] objectFromJSONString];
    if (result != nil && [result isKindOfClass:[NSArray class]]) {
        NSString *data = [request responseString];
        [self saveLocalData:data];
        [self loadView:data];
    }else{
        [self requestFailed:request];
    }
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    //是否存在本地数据
    NSString *local = [self getLocalData];
    if (local) {
        [self loadView:local];
    }else{
        [self clickAgainLoadingView:0];
    }
}

-(void)loadView:(NSString*)data
{
    NSArray *result = [data objectFromJSONString];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    for (NSDictionary *dic in result) {
        SportsGuideInfo *info = [[SportsGuideInfo alloc] init:dic];
        [array addObject:info];
    }
    self.dataArray = array;
    [self showView];
    [self hideProgressView];
}

@end
